package com.example.demo.config;

import com.example.demo.util.ConstVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute(ConstVariable.USER_SEESION_KEY)!=null){
            // 表示已经登录
            return true;
        }
        //未登录，跳转到登录页面
        response.sendRedirect("/login.html");
        return false;
    }
}
